package com.shop.dao.impl;

import com.shop.dao.CommentDao;
import com.shop.entity.Comment;
import com.shop.entity.CommentDetailVO;
import com.shop.exception.AddCommentErrorException;
import com.shop.exception.DeleteCommentErrorException;
import com.shop.exception.getCommentErrorException;
import com.shop.util.ConnectionUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CommentDaoImpl implements CommentDao {



    @Override
    public List<CommentDetailVO> queryAllCommentBy_GDID(int gd_id,int pageNum,int pageSize) throws getCommentErrorException {
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<CommentDetailVO> list = new ArrayList<CommentDetailVO>();
        try {
            con = ConnectionUtils.getConnection();
            String sql="SELECT COM.C_ID,COM.u_id,COM.GD_ID,u.u_name,COM.c_content,com.c_starttime,u.u_img\n" +
                    "FROM COMMENT COM\n" +
                    "JOIN good_detail GD ON COM.gd_id = GD.gd_id\n" +
                    "JOIN `user` u ON u.u_id = com.u_id\n" +
                    "WHERE GD.GD_ID= ? \n" +
                    "ORDER BY COM.C_ID DESC\n" +
                    "LIMIT ?,?";
            pstmt = con.prepareStatement(sql);
            pstmt.setInt(1,gd_id);
            pstmt.setInt(2,(pageNum-1)*pageSize);
            pstmt.setInt(3,pageSize);
            rs = pstmt.executeQuery();
            while(rs.next()){
                CommentDetailVO cdv = new CommentDetailVO();
                cdv.setC_id(rs.getInt("C_ID"));
                cdv.setU_id(rs.getInt("U_ID"));
                cdv.setC_content(rs.getString("c_content"));
                cdv.setC_starttime(rs.getString("c_starttime"));
                cdv.setGd_id(rs.getInt("GD_ID"));
                cdv.setU_name(rs.getString("U_NAME"));
                cdv.setImg_path(rs.getString("U_IMG"));
                list.add(cdv);
            }
            return list;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new getCommentErrorException(throwables);
        }finally {
            ConnectionUtils.close(rs,pstmt,con);
        }
    }

    @Override
    public int deleteCommentById(int c_id) throws DeleteCommentErrorException {
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        int n = -1;
        try {
            con = ConnectionUtils.getConnection();
            String sql = "DELETE FROM COMMENT WHERE C_ID = ?";
            pstmt = con.prepareStatement(sql);
            pstmt.setInt(1,c_id);
            n = pstmt.executeUpdate();
            return n;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new DeleteCommentErrorException(throwables);
        }finally {
            ConnectionUtils.close(rs,pstmt,con);
        }
    }

    @Override
    public int addComment(Comment comment) throws  AddCommentErrorException {
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        int n = -1;
        try {
            con = ConnectionUtils.getConnection();
            String sql = "INSERT INTO COMMENT (c_starttime,c_content,u_id,gd_id) values (?,?,?,?);";
            pstmt = con.prepareStatement(sql);

            Date date = new Date();
            String strDateFormat = "yyyy-MM-dd HH:mm:ss";
            SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);


            pstmt.setString(1, String.valueOf(sdf.format(date)));
            pstmt.setString(2,comment.getC_content());
            pstmt.setInt(3,comment.getU_id());
            pstmt.setInt(4,comment.getGd_id());
            n = pstmt.executeUpdate();
            return n;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            throw new AddCommentErrorException(throwables);
        }finally {
            ConnectionUtils.close(rs,pstmt,con);
        }
    }


}
